package com.zhijian.warehouse.manage.controller.app;

import com.zhijian.response.annotation.Response;
import com.zhijian.util.BeanUtil;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.entity.dto.UserDto;
import com.zhijian.warehouse.manage.entity.vo.request.*;
import com.zhijian.warehouse.manage.entity.vo.response.StoreAllotRecordProductVo;
import com.zhijian.warehouse.manage.entity.vo.response.StoreAllotRecordVo;
import com.zhijian.warehouse.manage.entity.vo.response.StoreVo;
import com.zhijian.warehouse.manage.enums.StockActionEnum;
import com.zhijian.warehouse.manage.holder.CurrentMpUserHolder;
import com.zhijian.warehouse.manage.holder.CurrentWebUserHolder;
import com.zhijian.warehouse.manage.service.StoreAllotRecordProductService;
import com.zhijian.warehouse.manage.service.StoreAllotRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 调拨单记录 控制层
 *
 * @author auto
 * @version 1.0
 * @date 2024-11-03 15:30:31
 */
@Slf4j
@Controller
@Api(tags = "app调拨单记录")
@RequestMapping("/app")
public class AppStoreAllotRecordController {

    @Autowired
    private StoreAllotRecordService service;

    @Autowired
    private StoreAllotRecordProductService allotRecordProductService;

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/store/allot/record")
    @ApiOperation("新增")
    public boolean insert(@RequestBody StoreAllotRecordSaveOrUpdateVo param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        UserDto user = CurrentMpUserHolder.getUser();
        param.setCreateUserId(user.getId());
        param.setUpdateUserId(user.getId());
        return service.save(param);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/store/allot/record/{id}")
    @ApiOperation("删除")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/store/allot/record/{id}")
    @ApiOperation("修改")
    public boolean update(@PathVariable("id") Long id, @RequestBody StoreAllotRecordSaveOrUpdateVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        UserDto user = CurrentMpUserHolder.getUser();
        param.setUpdateUserId(user.getId());
        param.setId(id);
        return service.modify(param);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/store/allot/record/{id}")
    @ApiOperation("根据id查询")
    public StoreAllotRecordVo selectById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(id);
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/store/allot/record/page")
    @ApiOperation("分页查询")
    public Page<StoreAllotRecordVo> selectByCondition(@RequestBody(required = false) StoreAllotRecordSearchVo param) {
        if (param == null)
            param = StoreAllotRecordSearchVo.builder().build();
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }

    /**
     * 列表查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/store/allot/record/list")
    @ApiOperation("列表查询")
    public List<StoreAllotRecordVo> selectList(@RequestBody(required = false) StoreAllotRecordSearchVo param) {
        if (param == null)
            param = StoreAllotRecordSearchVo.builder().build();
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectList(param);
    }



    /**
     * 提交审核
     *
     * @param param
     * @return
     */
    @Response
    @PutMapping("/store/allot/record/submitAudit")
    @ApiOperation("提交审核")
    public boolean submitAudit( @RequestBody StoreAllotRecordSaveOrUpdateVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), param);
        UserDto user = CurrentMpUserHolder.getUser();
        param.setUpdateUserId(user.getId());
        if (BeanUtil.isEmpty(param.getId()))
            param.setCreateUserId(user.getId());
        return service.submitAudit(param);
    }

    /**
     * 审核
     *
     * @param param
     * @return
     */
    @Response
    @PutMapping("/store/allot/record/audit")
    @ApiOperation("审核")
    public boolean audit( @RequestBody StoreAllotRecordSaveOrUpdateVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), param);
        UserDto user = CurrentMpUserHolder.getUser();
        param.setUpdateUserId(user.getId());
        return service.audit(param);
    }

    /**
     * 调拨出入库列表
     *
     * @param param
     * @param param
     * @return
     */
    @Response
    @PostMapping("/store/allot/product")
    @ApiOperation("调拨出入库列表  入参 status 0 出库 1 入库")
    public Page<StoreAllotRecordProductVo> selectProductPage(@RequestBody StoreAllotRecordProductSearchVo param) {
        log.info("{} - | selectProductPage info, {}", this.getClass().getSimpleName(), param);
        return allotRecordProductService.selectPage(param);
    }


    /**
     * 调拨出入库操作
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/store/allot/record/opt/{id}")
    @ApiOperation("出入库操作 id ：调拨出入库列表id")
    public boolean inOutOpt(@PathVariable("id") Long id, @RequestBody StoreAllotRecordProductInOrOutVo param) {
        log.info("{} - {}| inOutOpt info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return service.inOutOpt(param, StockActionEnum.OPT_SOURCE.APP.value);
    }


    /**
     * 获取最新的单号
     *
     * @return
     */
    @Response
    @GetMapping("/store/allot/record/next/number")
    @ApiOperation("获取最新的单号")
    public String createNextNumber() {
        return service.createNextNumber();
    }
}
